package com.courseSchedulingSystem.service.impl;

import com.courseSchedulingSystem.mapper.CommonMapper;
import com.courseSchedulingSystem.mapper.StudentMapper;
import com.courseSchedulingSystem.pojo.dto.StudentLoginDTO;
import com.courseSchedulingSystem.pojo.entity.*;
import com.courseSchedulingSystem.pojo.vo.StudentLoginVO;
import com.courseSchedulingSystem.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CommonMapper commonMapper;

    private StudentLoginDTO studentLoginDTO;


    @Override
    public StudentLoginVO login(StudentLoginDTO studentLoginDTO) {
        log.info("用户登录请求{}",studentLoginDTO);
        StudentLoginVO studentLoginVO = studentMapper.selectStudent(studentLoginDTO);
        if (studentLoginVO == null) {
            return null;
        }
        studentLoginVO.setStuId(studentLoginDTO.getStuId());
        return studentLoginVO;

    }

    public Boolean upDatePassword(String ordStuPassword, String newStuPassword) {
        if(studentLoginDTO.getStuPassword().equals(ordStuPassword)){
            studentMapper.upDatePassword(studentLoginDTO.getStuId(),newStuPassword);
            studentLoginDTO.setStuPassword(newStuPassword);
            return true;
        }else return false;
    }

//    public String signIn() {
//
//    }

    @Override
    public Student selectAllByStuId(int stuId) {
        Student student =studentMapper.selectAllByStuId(stuId);
        return student;
    }

    @Override
    public Map<String, String> getCurrentSemester() {
        log.info("获取当前学期信息");
        return studentMapper.getCurrentSemester();
    }

    /**
     * 学生选课
     */
    @Override
    public String selectCourse(String userId, String courseId) {
        log.info("用户 {} 选课: {}", userId, courseId);

        // 检查课程是否存在
        if (commonMapper.getCourse(courseId) == null) {
            return "课程不存在";
        }

        // 检查是否已经选了该课程
        boolean alreadySelected = studentMapper.isCourseSelected(userId, courseId);
        if (alreadySelected) {
            return "您已经选过此课程";
        }

        // 检查课程是否已满
        if (commonMapper.getCourse(courseId).getSelectedNum() >= commonMapper.getCourse(courseId).getMaxNum()) {
            return "课程人数已满";
        }

        // 4. 插入选课记录
        studentMapper.insertEnrollment(userId, courseId);
        return "选课成功";
    }

    /**
     * 学生退课
     */
    @Override
    public String dropCourse(String userId, String courseId) {
        log.info("用户 {} 退课: {}", userId, courseId);

        // 1. 检查是否选了该课程
        boolean alreadySelected = studentMapper.isCourseSelected(userId, courseId);
        if (!alreadySelected) {
            return "您尚未选此课程";
        }

        // 2. 删除选课记录
        studentMapper.deleteEnrollment(userId, courseId);
        return "退课成功";
    }

}
